Hack (programming Language)
   HOME

TheInfoList



OR:

Hack is a
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
for the
HipHop Virtual Machine HipHop Virtual Machine (HHVM) is an Open-source software, open-source virtual machine based on Just-in-time compilation, just-in-time (JIT) compilation that serves as an execution engine for the Hack (programming language), Hack programming lang ...
(HHVM), created by
Meta Meta (from the Greek μετά, '' meta'', meaning "after" or "beyond") is a prefix meaning "more comprehensive" or "transcending". In modern nomenclature, ''meta''- can also serve as a prefix meaning self-referential, as a field of study or ende ...
as a dialect of
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group ...
. The language implementation is
open-source Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product. The open-source model is a decentralized sof ...
, licensed under the
MIT License The MIT License is a permissive free software license originating at the Massachusetts Institute of Technology (MIT) in the late 1980s. As a permissive license, it puts only very limited restriction on reuse and has, therefore, high license comp ...
. Hack allows programmers to use both
dynamic typing In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
and
static typing In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
. This kind of a
type system In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer progra ...
is called
gradual typing Gradual typing is a type system in which some variable (computer science), variables and expressions may be given types and the correctness of the typing is checked at compile time (which is static typing) and some expressions may be left untyped ...
, which is also implemented in other programming languages such as
ActionScript ActionScript is an object-oriented programming language originally developed by Macromedia Inc. (later acquired by Adobe). It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript (meaning i ...
. Hack's type system allows types to be specified for
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-oriente ...
arguments An argument is a statement or group of statements called premises intended to determine the degree of truth or acceptability of another statement called conclusion. Arguments can be studied from three main perspectives: the logical, the dialectic ...
, function
return value In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is sa ...
s, and
class properties In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). In many languages, the class n ...
; however, types of
local variable In computer science, a local variable is a Variable (programming), variable that is given ''local scope (programming), scope''. A local variable reference in the subroutine, function or block (programming), block in which it is declared overrides ...
s are always inferred and cannot be specified.


History

Hack was introduced on March 20, 2014. Before the announcement of the new programming language, Facebook had already implemented the code and tested it on a large portion of its web site.


Features

Hack is designed to interoperate seamlessly with PHP, which is a widely used open-source
scripting language A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled. A scripting ...
that has a focus on
web development Web development is the work involved in developing a website for the Internet (World Wide Web) or an intranet (a private network). Web development can range from developing a simple single static page of plain text to complex web applications ...
and can be embedded into
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
. A majority of valid PHP scripts are also valid in Hack; however, numerous less frequently used PHP features and language constructs are not supported in Hack. Hack extends the type hinting available in PHP 5 through the introduction of static typing, by adding new type hints (for example, for scalar types such as integer or string), as well as by extending the use of type hints (for example, for class properties or function return values). However, types of local variables cannot be specified. Since Hack uses a gradual typing system, in the default mode, type annotations are not mandatory even in places they cannot be inferred; the type system will assume the author is correct and admit the code. However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.


Syntax and semantics

The basic file structure of a Hack script is similar to a PHP script with a few changes. A Hack file does not include the opening markup tag and forbids using top-level declarations. Code must be placed in an entrypoint function. These are automatically executed if they are in the top-level file, but not if the file is included via include, require, or the autoloader. Like other functions in Hack, the function names must be unique within a project – i.e. projects with multiple entrypoints can not call both main : <<__EntryPoint>> function main(): void The above script, similar to PHP, will be executed and the following output is sent to the browser: Hello, World! Unlike PHP, Hack and HTML code do not mix; either XHP or another template engine needs to be used.


Functions

Like PHP 7, Hack allows types to be specified for function arguments and function return values. Functions in Hack are thus annotated with types like the following: // Hack functions are annotated with types. function negate(bool $x): bool


See also

*
Parrot virtual machine Parrot was a register-based process virtual machine designed to run dynamic languages efficiently. It is possible to compile Parrot assembly language and Parrot intermediate representation (PIR, an intermediate language) to Parrot bytecode and ...
*
Phalanger ''Phalanger'' (from the Greek ''phalangion'', meaning spider's web, from their webbed (fused) toesChambers English Dictionary) is a genus of possums. Its members are found on New Guinea, the Maluku Islands, other nearby small islands, and Aust ...


References


External links

* and
Facebook Q&A: Hack brings static typing to PHP world

A tutorial on converting an existing PHP project to Hack

KPHP: An Executable Formal Semantics for PHP
{{Facebook navbox 2014 software Dynamically typed programming languages Facebook PHP software Programming languages created in 2014 Scripting languages Statically typed programming languages Programming languages compiled to bytecode